// manager/components/MultipleChoicePopup/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        disabled: {
			type: Boolean,
			value: false
		},
		list: {
			type: Array,
			value: function(){
				return []
			}
		},
		labelName: {
			required: true,
			type: String,
			value: ''
		},
		value: {
			required: true,
			type: [String, Number],
			value: ''
		},
		valueName: {
			required: true,
			type: String,
			value: ''
		},
		formKey: {
			required: true,
			type: String,
			value: ''
		},
    },

    /**
     * 组件的初始数据
     */
    data: {
        show: false,
        defaultValue: []
    },
    observers: {
        'value': function(value) {
            if(this.data.disabled){
                this.getSelectItem(value)
            }
        },
        'list': function(array) {
            if(this.data.disabled){
                this.getSelectItem(this.data.value)
            }
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        showPicker(){
			if(this.data.disabled){
				return
            }
            this.setData({
                show: true
            })
		},
		choice(e){
            let { item, index } = e.currentTarget.dataset;
            let { defaultValue, list } = this.data;
            if(item.isDisabled){
                return
            }
            console.log(defaultValue)
			let i = defaultValue.findIndex(c => c[this.data.valueName] == item[this.data.valueName]);
			if(i == -1){
                defaultValue.push(item)
                item.checked = true;
                list.splice(index,1,item)
			}else{
                defaultValue.splice(i,1)
                item.checked = false;
                list.splice(index,1,item)
            }
            this.setData({
                defaultValue,
                list
            })
            console.log(this.data.defaultValue)
            this.triggerEvent('confirm',{formKey: this.data.formKey,value: this.data.defaultValue})
		},
		cancel(e){
            let { list } = this.data;
            // list.map(item => {
            //     item.checked = false;
            //     return item
            // })
            this.setData({
                show: false,
                // defaultValue: [],
                // list
            })
			// this.getSelectItem(this.value)
		},
		confirm(e){
            this.setData({
                show: false,
            })
            this.triggerEvent('confirm',{formKey: this.data.formKey,value: this.data.defaultValue})
		},
		getSelectItem(value){
            let defaultValue = [];
            let { list } = this.data;
            let valueArr = value.split(",")
			if(list.length>0){
				list.forEach((item,index) => {
                    valueArr.forEach((id) => {
                        if(id == item[this.data.valueName]){
                            defaultValue.push(item)
                        }
                    })
				})
            }
            console.log(defaultValue)
            this.setData({
                defaultValue
            })
		}
    }
})